//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "vASupprimer.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CCheckB"
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
//                      Constructeur
//---------------------------------------------------------------------------
__fastcall TSupprimer::TSupprimer(const int &nLogiciel,
                                  const int &nVersion,
                                  TStringList * ListRef,
                                  bool bMultiple,
                                  TComponent* Owner)
    : TForm(Owner)
{
    try {
        m_nLogiciel = nLogiciel;
        m_nVersion  = nVersion;
        m_ListRef = ListRef;
        AnsiString asRef = "";

        if(m_ListRef->Count > 1) {
            for(int i=0; i<m_ListRef->Count; i++) {
                asRef += m_ListRef->Strings[i]+",";
            }
            asRef += "...";
        }else {
            asRef = m_ListRef->Strings[0];
        }

        if(bMultiple) {
            lAttention->Caption = MSG_ATTENTION_ADRESSE(asRef);
            bbVisualiser->Visible = true;
        }else {
            lAttention->Caption = MSG_ATTENTION(asRef);
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//                  Activer le bouton Oui
//---------------------------------------------------------------------------
void __fastcall TSupprimer::chkEnableYesClick(TObject *Sender)
{
    try {
        if(chkEnableYes->Checked == true)
            bbOui->Enabled = true;
        else
            bbOui->Enabled = false;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//                  Confirmer la suppression
//---------------------------------------------------------------------------
void __fastcall TSupprimer::bbOuiClick(TObject *Sender)
{
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    try {
            dmWebHelp = new TdmWebHelp((TComponent*)0);

            MaRequete = new TZZMySqlQuery( (TComponent*)0);
            MaRequete->Database = dmWebHelp->dbWebHelp;
            for(int i=0; i<m_ListRef->Count; i++) {
                MaRequete->Sql->Text =  (AnsiString)  "delete from refer "+
                                                      "where (log_code ="+m_nLogiciel+" and ver_code="+m_nVersion+
                                                      ") and ref_nom=\'"+m_ListRef->Strings[i]+"\'";
                MaRequete->ExecSql();
                MaRequete->Sql->Clear();

                MaRequete->Sql->Text = "Commit";
                MaRequete->ExecSql();
                MaRequete->Sql->Clear();
            }

            MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//                  Visualiser les références
//---------------------------------------------------------------------------
void __fastcall TSupprimer::bbVisualiserClick(TObject *Sender)
{

    TuGestionAdresse * uGestionAdresse = NULL;
    try {
        AnsiString asConditionIn = "";
        //Préparation de la requete SQL condition IN('valeur1','valeur2',...)
        int i=0;
        if(m_ListRef->Count > 1) {
            for(i=0; i<m_ListRef->Count-1; i++) {
                asConditionIn += "\'"+m_ListRef->Strings[i]+"\',";
            }
            asConditionIn += "\'"+m_ListRef->Strings[i]+"\'";
        }else {
            asConditionIn = "\'"+m_ListRef->Strings[0]+"\'";
        }

        bAfficheBoutonsDuBas = false;

        uGestionAdresse = new TuGestionAdresse((TComponent*)0);
        uGestionAdresse->SetConditionIn(asConditionIn);
        for(int j=0;j<uGestionAdresse->cbLogiciel->Items->Count;j++) {
            if(m_nLogiciel == (int)uGestionAdresse->cbLogiciel->Items->Objects[j]) {
                uGestionAdresse->cbLogiciel->ItemIndex = j;
                break;
            }
        }

        uGestionAdresse->cbLogicielChange(uGestionAdresse->cbLogiciel->Items->Objects[uGestionAdresse->cbLogiciel->ItemIndex]);

        for(int k=0;k<uGestionAdresse->cbVersion->Items->Count;k++) {
            if(m_nVersion == (int)uGestionAdresse->cbVersion->Items->Objects[k]){
                uGestionAdresse->cbVersion->ItemIndex = k;
                break;
            }
        }

        uGestionAdresse->cbVersionChange(uGestionAdresse->cbVersion->Items->Objects[uGestionAdresse->cbVersion->ItemIndex]);

        uGestionAdresse->ShowModal();
        if(uGestionAdresse) {delete uGestionAdresse; uGestionAdresse = NULL;}
        bAfficheBoutonsDuBas = true;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(uGestionAdresse) {delete uGestionAdresse; uGestionAdresse = NULL;}
}
//---------------------------------------------------------------------------
